<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
	if (isset($_COOKIE["studial"]))
	{
	}
	else
	{
		$url = "/";
		$expire = time() + 60*60*24*7;
		setcookie("studial", "", $expire, $url);
	}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Login</title>
		<script language="javascript" type="text/javascript">
            function Highlight_Box_On(current)
            {	
                document.getElementById(current).style.backgroundColor = "#00EEFF";
            }
            function Highlight_Box_Off(current)
            {
                document.getElementById(current).style.backgroundColor = "white";
            }
        </script>
</head>

<body>
		<?php
			if (isset($_COOKIE["studial"]))
			{
				$ini_array = parse_ini_file("../config.ini");
				@ $con = mysql_pconnect($ini_array['dblink'], $ini_array['dbuser'], $ini_array['dbpass']);
					
				$Email = $_COOKIE['studial'];
				
				if (!$con)
				{
					die('Could not connect: ' . mysql_error());
				}
				
				@ mysql_set_charset('utf8');
				@ mysql_select_db($ini_array['dbname']);
		
				$query = "SELECT * FROM `users` u WHERE u.primaryemail = '".$Email."'";
				$result = mysql_query($query) or die('Error, query failed');
				
				if( mysql_num_rows($result) != 0 )
				{
					header('location: ../personal/home.php');
					exit;
				}
				else
				{
		?>
        
        <form action="check_login.php" method="post">
            <table bordercolor="#0000CC" style="margin-top:5%; margin-left:55%;">
            	<tr>
                	<td align="center"><b>SIGN INTO STUDIAL</b></td>
                </tr>
                <tr>
                	<td><hr /></td>
                </tr>
                <tr>
                	<td align="center">Don't have a Studial account?</td>
                </tr>
                <tr>
                	<td align="center"><a href="../register/form.php"><input type="button" value="Create New Account" style="background-color:#FFFF00" /></a></td>
                </tr>
                <tr>
                	<td><hr /></td>
                </tr>
                <tr>
                    <td>Email:</td>
                </tr>
                <tr>
                    <td><input type="text" name="User" id="User" onfocus="Highlight_Box_On('User')" onblur="Highlight_Box_Off('User')" maxlength="15" /></td>
                </tr>
                <tr>
                    <td>(e.g. itec@itec.hcmus.edu.vn)</td>
                </tr>
                <tr>
                    <td>Password:</td>
                </tr>
                <tr>
                    <td><input type="password" name="Pass" id="Pass" onfocus="Highlight_Box_On('Pass')" onblur="Highlight_Box_Off('Pass')" maxlength="100" /></td>
                </tr>
                <tr align="center">
                    <td colspan="2"><input type="submit" value="Sign In" style="background-color:#CCCCCC" /></td>
                </tr>
                <tr>
                	<td><input type="checkbox" name="KeepLog" value="1" checked="checked" />Keep me signed in</td>
                </tr>
                <tr>
                	<td>(Uncheck if on a shared computer)</td>
                </tr>
                <tr>
                	<td><hr /></td>
                </tr>
                <tr>
                	<td><div align="center"><a href="forget_pw.html">I cannot access my account</a></div></td>
                </tr>
            </table>
        </form>
        
        <?php
					exit;
				}
			}
			else
			{
		?>
        
        <form action="check_login.php" method="post">
            <table bordercolor="#0000CC" style="margin-top:5%; margin-left:55%;">
            	<tr>
                	<td align="center"><b>SIGN INTO STUDIAL</b></td>
                </tr>
                <tr>
                	<td><hr /></td>
                </tr>
                <tr>
                	<td align="center">Don't have a Studial account?</td>
                </tr>
                <tr>
                	<td align="center"><a href="../register/form.php"><input type="button" value="Create New Account" style="background-color:#FFFF00" /></a></td>
                </tr>
                <tr>
                	<td><hr /></td>
                </tr>
                <tr>
                    <td>Email:</td>
                </tr>
                <tr>
                    <td><input type="text" name="User" id="User" onfocus="Highlight_Box_On('User')" onblur="Highlight_Box_Off('User')" maxlength="15" /></td>
                </tr>
                <tr>
                    <td>(e.g. itec@itec.hcmus.edu.vn)</td>
                </tr>
                <tr>
                    <td>Password:</td>
                </tr>
                <tr>
                    <td><input type="password" name="Pass" id="Pass" onfocus="Highlight_Box_On('Pass')" onblur="Highlight_Box_Off('Pass')" maxlength="100" /></td>
                </tr>
                <tr align="center">
                    <td colspan="2"><input type="submit" value="Sign In" style="background-color:#CCCCCC" /></td>
                </tr>
                <tr>
                	<td><input type="checkbox" name="KeepLog" value="1" checked="checked" />Keep me signed in</td>
                </tr>
                <tr>
                	<td>(Uncheck if on a shared computer)</td>
                </tr>
                <tr>
                	<td><hr /></td>
                </tr>
                <tr>
                	<td><div align="center"><a href="forget_pw.html">I cannot access my account</a></div></td>
                </tr>
            </table>
        </form>
        
        <?php
				exit;
			}
		?>
</body>
</html>